package com.motong.gongdan.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Auther: FreddieFang
 * @Date: 2021/6/25 15:54
 * @Description:
 */
public class TimeUtil {

    /** yyyy-MM-dd HH:mm:ss */
    public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /** yyyy-MM-dd */
    public static String DEFAULT_DAY_FORMAT = "yyyy-MM-dd";
    /** yyyyMMddHHmmss */
    public static String DEFAULT_DATE_NO_SEPRATOR_FORMAT = "yyyyMMddHHmmss";
    /** yyyyMMdd */
    public static final String DEFAULT_DAY_NO_SEPRATOR_FORMAT = "yyyyMMdd";
    /** dd/MM/yyyy */
    public static final String DEFAULT_SLASH_FORMAT = "dd/MM/yyyy";

    /**
     * 指定日期格式，转化时间字符串为Date对象
     *
     * @param pattern
     * @param dateString
     * @return
     */
    public static Date parseDate(String pattern, String dateString) {
        DateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 把yyyy-MM-dd格式的时间字符串转化时间为Date对象
     *
     * @param
     * @param dateString
     * @return
     */
    public static Date parseDateToDay(String dateString) {
        if (StringUtil.isBlank(dateString)) {
            return null;
        }
        return parseDate(DEFAULT_DAY_FORMAT, dateString);

    }
    public static Date parseDateToDayb(String dateString) {
        if (StringUtil.isBlank(dateString)) {
            return null;
        }
        return parseDate(DEFAULT_DATE_FORMAT, dateString);

    }

    public static Date parseDateToDayc(String dateString) {
        if (StringUtil.isBlank(dateString)) {
            return null;
        }
        return parseDate("yyyy-MM-dd HH:mm", dateString);

    }


    /**
     * 指定日期格式，获取当前时间的Date对象
     *
     * @param pattern
     * @param
     * @return
     */
    public static Date getCurrentDate(String pattern) {
        if (StringUtil.isBlank(pattern)) {
            return null;
        }
        DateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(new Date().toString());
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 返回当前时间的 yyyy-MM-dd HH:mm:ss字符串
     *
     * @return
     */
    public static String getCurrentDateDefaultString() {
        return getCurrentDateString(DEFAULT_DATE_FORMAT);
    }

    /**
     * 返回当前时间的指定格式的字符串
     *
     * @return
     */
    public static String getCurrentDateString(String pattern) {
        if (StringUtil.isBlank(pattern)) {
            return null;
        }
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date());
    }

    /**
     * 返回指定时间的yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @return
     */
    public static String getDateDefaultString(Date date) {
        if (date == null) {
            return null;
        }
        return getDateString(date, DEFAULT_DATE_FORMAT);
    }


    public static String getDateDefaultStringTwo(Date date) {
        if (date == null) {
            return null;
        }
        return getDateString(date, DEFAULT_DAY_FORMAT);
    }



    /**
     * 返回指定时间的指定格式的字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateString(Date date, String pattern) {
        if (date == null || StringUtil.isBlank(pattern)) {
            return null;
        }
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * 返回指定时间的指定格式的字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateString(Date date, String pattern,
                                       String timeZone) {
        if (date == null || StringUtil.isEmpty(pattern)) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        return dateFormat.format(date);
    }

    /**
     * 比较日期是否大于当前日期
     */
    public static boolean afterNow(Date date) {
        if (date == null) {
            return false;
        }

        Calendar nowCar = Calendar.getInstance();
        Calendar car = Calendar.getInstance();

        car.setTime(date);

        return car.after(nowCar);
    }

    /**
     * 获取系统当前日期(精确到毫秒)，格式：yyyyMMddHHmmss
     *
     * @return
     */
    public static String getDateStringWithNoSeparator() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat(DEFAULT_DATE_NO_SEPRATOR_FORMAT);
        return df.format(date);
    }

    /**
     * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
     *
     * @return
     */
    public static String getDate() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat(DEFAULT_DAY_NO_SEPRATOR_FORMAT);
        return df.format(date);
    }

    public static String getDateTwo() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM");
        return df.format(date);
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String getTime(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        return dateFormat.format(date);

    }

    public static String getTime(Date date, String timeZoneID) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        return dateFormat.format(date);

    }

    public static String getDate(String dateformat, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
        return dateFormat.format(date);
    }

    public static int getWeekByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekDay == Calendar.MONDAY) {
            return 1;
        } else if (weekDay == Calendar.TUESDAY) {
            return 2;
        } else if (weekDay == Calendar.WEDNESDAY) {
            return 3;
        } else if (weekDay == Calendar.THURSDAY) {
            return 4;
        } else if (weekDay == Calendar.FRIDAY) {
            return 5;
        } else if (weekDay == Calendar.SATURDAY) {
            return 6;
        } else if (weekDay == Calendar.SUNDAY) {
            return 7;
        }
        return weekDay;
    }

    public static int getWeekByDate(Date date, String timeZoneID) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekDay == Calendar.MONDAY) {
            return 1;
        } else if (weekDay == Calendar.TUESDAY) {
            return 2;
        } else if (weekDay == Calendar.WEDNESDAY) {
            return 3;
        } else if (weekDay == Calendar.THURSDAY) {
            return 4;
        } else if (weekDay == Calendar.FRIDAY) {
            return 5;
        } else if (weekDay == Calendar.SATURDAY) {
            return 6;
        } else if (weekDay == Calendar.SUNDAY) {
            return 7;
        }
        return weekDay;
    }

    public static long getTimeMill(Date date, String timeZoneID) {
        return date.getTime() - TimeUtil.getDate(date, timeZoneID).getTime();
    }

    public static Date getDate(Date date, String timeZoneID) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtil.isEmpty(timeZoneID)) {
            dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        }
        String dateStr = dateFormat.format(date);
        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static long getTimeMill(Date date) {
        return date.getTime() - TimeUtil.getDate(date, null).getTime();
    }

    public static int getMonthMaxDay(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static int getMonthMaxDay(Date date, String timeZoneID) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static long getDateMill(Date date) {
        return date.getTime() - getTimeMill(date);
    }

    public static long getDateMill(Date date, String timeZoneID) {
        return TimeUtil.getDate(date, timeZoneID).getTime();
    }

    /**
     * 取得日期时间里的日期
     *
     * @param dateTime
     * @return
     */
    public static Date getDate(Date dateTime) {
        long nowMill = dateTime.getTime();
        long dateMill = nowMill - TimeUtil.getTimeMill(dateTime);
        return new Date(dateMill);
    }

    /**
     * Calculate date
     *
     * @param date
     * @param interval
     * @param calType
     *            "Y":year "M":month "D":day
     * @return
     */
    public static Date calDate(Date date, int interval, String calType) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);

        if ("Y".equals(calType)) {
            rightNow.add(Calendar.YEAR, interval);
            return rightNow.getTime();
        }

        if ("M".equals(calType)) {
            rightNow.add(Calendar.MONTH, interval);
            return rightNow.getTime();
        }

        if ("D".equals(calType)) {
            rightNow.add(Calendar.DAY_OF_YEAR, interval);
            return rightNow.getTime();
        }
        return date;
    }

    /**
     * @return yyyy-MM-dd 00:00:00
     */
    public static Date getToday() {
        String date = getCurrentDateDefaultString();
        date = date.substring(0, 10) + " 00:00:00";
        return parseDateToDay(date);
    }

    /**
     * 将时间转换为指定时区的时间
     *
     * @param date
     * @param timeZoneId
     *            值为 CMT-8,CMT+3等
     * @return
     */
    public static Date transferByTimeZone(Date date, String timeZoneId) {
        if (date == null || StringUtil.isBlank(timeZoneId)) {
            return null;
        }
        try {
            int diffTime = TimeZone.getDefault().getRawOffset()
                    - TimeZone.getTimeZone(timeZoneId).getRawOffset();
            long nowTime = date.getTime();
            long newNowTime = nowTime - diffTime;
            return new Date(newNowTime);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 取得时间里面的月份
     *
     * @param date
     * @return
     */
    public static int getDateMonth(Date date, String timeZoneID) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 取得日期的天
     *
     * @param date
     * @return
     */
    public static int getDateDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 取得日期的天
     *
     * @param date
     * @return
     */
    public static int getDateDay(Date date, String timeZoneID) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        return calendar.get(Calendar.DATE);
    }

    /**
     * 获得某年某月的第几个星期几的 日期
     *
     * @param year
     * @param month
     * @param weekInMonth
     * @param dayInWeek
     * @return
     */
    public static Date getDate(int year, int month, int weekInMonth,
                               int dayInWeek) {
        Calendar date = Calendar.getInstance();
        date.clear();
        date.set(Calendar.YEAR, year);
        date.set(Calendar.MONTH, month - 1);
        date.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekInMonth);
        date.set(Calendar.DAY_OF_WEEK, dayInWeek + 1);
        return date.getTime();
    }

    /**
     * 获得某年某月的第几个星期几的 日期
     *
     * @param year
     * @param month
     * @param weekInMonth
     * @param dayInWeek
     * @return
     */
    public static Date getDate(int year, int month, int weekInMonth,
                               int dayInWeek, String timeZoneID) {
        Calendar date = Calendar.getInstance();
        date.clear();
        date.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        date.set(Calendar.YEAR, year);
        date.set(Calendar.MONTH, month - 1);
        date.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekInMonth);
        date.set(Calendar.DAY_OF_WEEK, dayInWeek + 1);
        return date.getTime();
    }

    /**
     * 取得日期里面的 年份
     *
     * @param date
     * @return
     */
    public static int getDateYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getDateMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH);
    }

    /**
     * 取得日期里面的 年份
     *
     * @param date
     * @return
     */
    public static int getDateYear(Date date, String timeZoneID) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 取得某年某月的最后一个星期几的 日期
     *
     * @param year
     * @param month
     * @param dayInWeek
     * @return
     */
    public static Date getLastWeekDayDate(int year, int month, Integer dayInWeek) {
        Date fourthDate = getDate(year, month, 4, dayInWeek);
        int fourthDateDay = TimeUtil.getDateDay(fourthDate);
        int maxDateDay = TimeUtil.getMonthMaxDay(fourthDate);
        if ((fourthDateDay + 7) <= maxDateDay) {
            return new Date(fourthDate.getTime() + 7 * 24 * 60 * 60 * 1000l);
        } else {
            return fourthDate;
        }
    }

    /**
     * 取得某年某月的最后一个星期几的 日期
     *
     * @param year
     * @param month
     * @param dayInWeek
     * @return
     */
    public static Date getLastWeekDayDate(int year, int month,
                                          Integer dayInWeek, String timeZoneID) {
        Date fourthDate = getDate(year, month, 4, dayInWeek);
        int fourthDateDay = TimeUtil.getDateDay(fourthDate, timeZoneID);
        int maxDateDay = TimeUtil.getMonthMaxDay(fourthDate, timeZoneID);
        if ((fourthDateDay + 7) <= maxDateDay) {
            return new Date(fourthDate.getTime() + 7 * 24 * 60 * 60 * 1000l);
        } else {
            return fourthDate;
        }
    }

    public static Date parseDate(String pattern, String dateTimeStr,
                                 String timeZoneID) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
        return dateFormat.parse(dateTimeStr);
    }

    public static String convertWeekNumWithLanguage(int weekNum, String language) {
        switch (weekNum) {
            case 1:
                if ("EN".equals(language)) {
                    return "Monday";
                } else if ("CN".equals(language)) {
                    return "周一";
                }
                break;
            case 2:
                if ("EN".equals(language)) {
                    return "Tuesday";
                } else if ("CN".equals(language)) {
                    return "周二";
                }
                break;
            case 3:
                if ("EN".equals(language)) {
                    return "Wednesday";
                } else if ("CN".equals(language)) {
                    return "周三";
                }
                break;
            case 4:
                if ("EN".equals(language)) {
                    return "Thursday";
                } else if ("CN".equals(language)) {
                    return "周四";
                }
                break;
            case 5:
                if ("EN".equals(language)) {
                    return "Friday";
                } else if ("CN".equals(language)) {
                    return "周五";
                }
                break;
            case 6:
                if ("EN".equals(language)) {
                    return "Saturday";
                } else if ("CN".equals(language)) {
                    return "周六";
                }
                break;
            case 7:
                if ("EN".equals(language)) {
                    return "Sunday";
                } else if ("CN".equals(language)) {
                    return "周日";
                }
                break;
        }
        return "";
    }

    public static List<String> getTimeDurations() {
        List<String> durationArray = new ArrayList<String>();
        float duration = 0.5f;
        for (; duration <= 24; duration += 0.5) {
            if (duration - (int) (duration) > 0) {
                durationArray.add(duration + "");
            } else {
                durationArray.add((int) (duration) + "");
            }

        }
        return durationArray;
    }

    // 获得当天0点时间
    public static Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得当天24点时间
    public static Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
                cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }

    // 获得本月第一天0点时间
    public static Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
                cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获得本月最后一天24点时间
    public static Date getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
                cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    // 获得本月最后一天24点时间
    public static Date getTimesMonthnightNew() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),
                cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        return cal.getTime();
    }

    //获取某月最后一天
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());

        return lastDayOfMonth;
    }

    /**
     * 当天最早时间
     *
     * @return
     */
    public static String getDayEarlyTime() {
        Date newDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newStr = sdf.format(newDate);
        newStr = newStr + " 00:00:00";
        return newStr;
    }

    /**
     * 当天最晚时间
     *
     * @return
     */
    public static String getDayLateTime() {
        Date newDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newStr = sdf.format(newDate);
        newStr = newStr + " 23:59:59";
        return newStr;
    }

    /**
     * 获取多少天之后的日期
     *
     * @param numberDay
     * @return yyyy-MM-dd
     */
    public static String getFutureDayTime(int numberDay) {
        Date newDate = new Date();
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(newDate);
        theCa.add(theCa.DATE, numberDay);
        Date oldDate = theCa.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String oldStr = sdf.format(oldDate);
        return oldStr;
    }

    public static String getFutureDayTimeOther(int numberDay) {
        Date newDate = new Date();
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(newDate);
        theCa.add(theCa.DATE, numberDay);
        Date oldDate = theCa.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String oldStr = sdf.format(oldDate);
        return oldStr;
    }


    /**
     * 计算两个时间时间相隔的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2){ // bu同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                {
                    timeDistance += 366;
                } else // 不是闰年
                {
                    timeDistance += 365;
                }
            }
            if(timeDistance + (day2 - day1)==0){
                for (int i = year2; i < year1; i++) {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                    {
                        timeDistance += 366;
                    } else // 不是闰年
                    {
                        timeDistance += 365;
                    }
                }
            }

            return timeDistance + (day2 - day1);
        } else // 同年
        {
            return day2 - day1;
        }
    }

    /**
     * 计算时间与当前时间的秒数
     */
    public static boolean getTimeMiao(Date time){
        Date b = new Date();
        Date a = time;
        Integer interval = (int) ((b.getTime() - a.getTime())/1000);
        int c = 60*60*3;
        System.out.println(interval);
        if(interval > c){
            return false;
        }else{
            return true;
        }
    }

    /**
     *   获取当前时间之前或之后几小时时间
     * @param hour
     * @return
     */
    public static String getTimeByHour(Integer hour,Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static String getTimeByDay(Integer day,Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static String getTimeByDayTwo(Integer day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static String getTimeByMonth(Integer month){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, month);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }
    public static String getTimeByYear(Integer year){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, year);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static String getTimeByHourh(Integer hour){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, hour);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }


    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


//	public static void main(String[] args) {
////		System.out.println(getDateString(getTimesMonthmorning(), "yyyy-MM-dd"));
////		System.out.println(getDateString(getTimesMonthnight(), "yyyy-MM-dd"));
//
//		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//	        try {
//				System.out.println(getDaysOfMonth(sdf.parse("2017-01-08")));
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//	}

    /**
     * 取得当月天数
     * */
    public static int getCurrentMonthLastDay()
    {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    private static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date parse(String dateStr)
            throws ParseException
    {
        return parse(dateStr, DEFAULT_PATTERN);
    }
    public static Date parse(String dateStr, String pattern)
            throws ParseException
    {
        if ((dateStr == null) || ("".equals(dateStr))) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(dateStr);
    }


    /**
     * 判断时间是否在某个时间段内
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取过去时间点
     * @param type
     * @return
     */
    public static Date getLastPeroid(int type, Date date,int q){
        Calendar c = Calendar.getInstance();
        if (0 == type){
            //过去1天
            c.setTime(date);
            c.add(Calendar.DATE, -1);
            Date d = c.getTime();
            return d;
        }else if (1 == type){
            //过去7天
            c.setTime(date);
            c.add(Calendar.DATE, -7);
            Date d = c.getTime();
            return d;
        }else if (2 == type){
            //过去一月
            c.setTime(date);
            c.add(Calendar.MONTH, -q);
            Date m = c.getTime();
            return m;
        }else if (3 == type){
            //过去一年
            c.setTime(date);
            c.add(Calendar.YEAR, -1);
            Date y = c.getTime();
            return y;
        }else if (4 == type){
            //过去二十年
            c.setTime(date);
            c.add(Calendar.YEAR, -20);
            Date y = c.getTime();
            return y;
        }
        //过去1天
        c.setTime(date);
        c.add(Calendar.DATE, -1);
        Date d = c.getTime();
        return d;
    }

    /**
     * 当年第一天
     *
     * @param date
     *            日期
     * @return 天
     * @throws Exception
     *             异常
     */
    public static String getThisYear(String date) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy");
        Date time = format3.parse(date);
        String time1 = format3.format(time);
        Date startTime = format.parse(time1 + "-01-01");
        String dates = format.format(startTime);
        return dates;
    }

    /**
     * 	获取两个月份之间的所有月份(含跨年)
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetween(String minDate, String maxDate) {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }

            // 实现排序方法
            Collections.sort(result, new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                    String str1 = (String) o1;
                    String str2 = (String) o2;
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
                    Date date1 = null;
                    Date date2 = null;
                    try {
                        date1 = format.parse(str1);
                        date2 = format.parse(str2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    if (date2.compareTo(date1) > 0) {
                        return -1;
                    }
                    return 1;
                }
            });
        }catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String getStringDateBefore(String dateString,Integer month){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Calendar now = Calendar.getInstance();
        now.setTime(TimeUtil.parseDate("yyyy-MM",dateString));
        now.add(Calendar.MONTH, -month);
        Date before = now.getTime();
        return dateFormat.format(before);
    }

    public static void main(String[] args) {
        System.out.println(TimeUtil.getDateDefaultStringTwo(new Date()));
    }

    public static double getDateSubMinutes(String time, String timeOne) {
        //计算相差分钟数，并保留两位小数
        long seconds = getDateSubSeconds(time, timeOne);
        BigDecimal b = new BigDecimal(seconds / 60.0D);
        return b.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
    public static long getDateSubSeconds(String time, String timeOne) {
        //计算秒数
        LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime localDateTimeOne = LocalDateTime.parse(timeOne, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return Duration.between(localDateTime, localDateTimeOne).toMillis() / 1000;
    }

    /**
     * 两个日期相隔的日期
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 昨天
     */
    public static String beforeOneDay(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
        String ZUOTIAN=sp.format(d);
        return ZUOTIAN;
    }

    /**
     * 当月第一天
     */
    public static String getMonthFirstDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        return first;
    }

    /**
     * 判断日期是否为该月第一天
     */
    public static boolean isFirstDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }


    /**
     * 根据传入的参数，来对日期区间进行拆分，返回拆分后的日期List
     * @param statisticsType
     * @param map
     * @return
     * @throws ParseException
     * @editor
     * @editcont
     */
    public static List<String> doDateByStatisticsType(String statisticsType,Map<String, Object> map) throws ParseException{
        List<String> listWeekOrMonth = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        Date sDate = dateFormat.parse(startDate);
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        sCalendar.setTime(sDate);
        Date eDate = dateFormat.parse(endDate);
        Calendar eCalendar = Calendar.getInstance();
        eCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        eCalendar.setTime(eDate);
        boolean bool =true;
        if(statisticsType.equals("week")){
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_WEEK)==2||sCalendar.get(Calendar.DAY_OF_WEEK)==1){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }else{
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_MONTH)==1||sCalendar.get(Calendar.DAY_OF_MONTH)==sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }

        return listWeekOrMonth;
    }


    public static String differ(Date date1,Date date2){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l=date2.getTime()-date1.getTime();
        return  cnFormatByTime(l);
//        long day=l/(24*60*60*1000);
//        long hour=(l/(60*60*1000)-day*24);
//        long min=((l/(60*1000))-day*24*60-hour*60);
//        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
//        return day+"天"+hour+"小时"+min+"分"+s+"秒";
    }

    public static String cnFormatByTime( long l){
        long day=l/(24*60*60*1000);
        long hour=(l/(60*60*1000)-day*24);
        long min=((l/(60*1000))-day*24*60-hour*60);
        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
        return day+"天"+hour+"小时"+min+"分"+s+"秒";
    }

    public static String cnFormatByTimeExpectDay( long l){
        long hour=(l/(60*60*1000));
        long min=((l/(60*1000))-hour*60);
        long s=(l/1000-hour*60*60-min*60);
        return hour+"小时"+min+"分"+s+"秒";
    }

    //获取前一天最早时间
    public static Date getBeforeDayStartTime() throws ParseException {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beforeOneDay = sdf.format(cal.getTime()); //得到前一天的时间
        String startTime = beforeOneDay.substring(0, 10) + " 00:00:00";

        Date beforeDayStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
        return beforeDayStartTime;
    }
    //获取前一天最晚时间
    public static Date getBeforeDayEndTime() throws ParseException {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beforeOneDay = sdf.format(cal.getTime()); //得到前一天的时间
        String endTime = beforeOneDay.substring(0, 10) + " 23:59:59";
        Date beforeDayStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        return beforeDayStartTime;
    }

    //获取前7天最早时间
    public static Date getBeforeWeekStartTime() throws ParseException {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beforeOneDay = sdf.format(cal.getTime()); //得到前一周的时间
        String startTime = beforeOneDay.substring(0, 10) + " 00:00:00";

        Date beforeDayStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
        return beforeDayStartTime;
    }

    //获取前7天最晚时间
    public static Date getBeforeWeekEndTime() throws ParseException {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beforeOneDay = sdf.format(cal.getTime()); //得到前7天的时间
        String endTime = beforeOneDay.substring(0, 10) + " 23:59:59";
        Date beforeDayStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        return beforeDayStartTime;
    }




}
